package org.example.fronted.controller;

import org.example.entity.Address;
import org.example.entity.Data;
import org.example.entity.User;
import org.example.fronted.service.AddressService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
//@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService2 addressService2;

//    public AddressController(AddressService2 addressService2) {
//        this.addressService2 = addressService2;
//    }

    @Resource
    HttpSession httpSession;
    public AddressController(){};

    /**
     * 根据aid修改当前地址为收货地址
     * @param addressId
     * @return
     */
    @RequestMapping("/changeTakeDeliveryAddress")
    public Data changeTakeDeliveryAddress(Integer addressId){
        //httpSession.setAttribute("uId",1);
        Integer uId = (Integer)httpSession.getAttribute("uId");
        // aId = 1;
        uId = 28;
        System.out.println("aId:" + addressId);
        Data data = addressService2.changeTakeDeliveryAddress(uId, addressId);

        return data;
    }

    @RequestMapping("/changeDefaultAddress")
    public int changeDefaultAddress(Integer aId){

        //httpSession.setAttribute("uId",1);//todo 自定的UID
        Integer uId = (Integer)httpSession.getAttribute("uId");
        return addressService2.defaultAddress(uId, aId);
    }

    /**
     *  获取当前用户的收货地址
     * @param req
     */
    @RequestMapping("/getAddressOfUser")
    public List<Address> getAddressOfUser(HttpServletRequest req) {
        HttpSession session = req.getSession();

        int userId = (int)session.getAttribute("userId");

        System.out.println(userId);
//        Integer userId = user.getId();  //获取用户id
//        userId = 1;  //获取用户id

        // 获取当前用户的收货地址
        List<Address> addresses = addressService2.getAddressByUserId(userId);
        System.out.println(addresses);

        return addresses;

    }
}
